<template><view></view></template>

<script>
const app = getApp();

import { text2Audio } from '@/api/others.js';
export default {
    data() {
        return {
            playingList: [],
            playing: false,
            playingIndex: -1,
            audioContext: '',
            listener: '',
            audioContext2: ''
        };
    },
    props: {},
	mounted() {
		this.initAudioPlayer();
		this.startListener();
	},
    methods: {
        initAudioPlayer() {
			this.audioContext = uni.createInnerAudioContext();
            this.audioContext.onPlay(() => {
                uni.hideLoading();
            });
            this.audioContext.onEnded(() => {
				this.playing = false;
            });
            this.audioContext.onError((res) => {
				this.playing = false;
				uni.showToast({
					title:'播放失败',
					icon:'none',
					duration:2000
				})
            });
			// console.log(this.audioContext,'1111111111111')
        },

        addToList(text) {
            if (text) {
                let playingList = this.playingList;
                playingList.push({
                    text: text,
                    audio: '',
                    state: 0
                });
            }
        },

        startListener() {
            let listener = setInterval(() => {
                // 有新的需要播放的，开始播放
                let playingList = this.playingList;
                let playingIndex = this.playingIndex;
                if (!playingList[playingIndex + 1]) {
                    return;
                }
                if (!playingList[playingIndex + 1].audio) {
                    if (playingList[playingIndex + 1].state == 0) {
                        if (playingList[playingIndex + 1].text != '[end]') {
							// console.log('?????')
                            this.text2Audio(playingIndex + 1);
                        } else if (!this.playing) {
                            this.stopPlay();
                        }
                    }
                } else if (!this.playing) {
					this.playingIndex = playingIndex + 1;
                    this.startPlay();
                }
            }, 200);
			this.listener = listener;
        },

        stopListener() {
            clearInterval(this.listener);
        },

        startPlay() {
			this.playing = true;
            const playingList = this.playingList;
            const playingIndex = this.playingIndex;
            this.audioContext.src = playingList[playingIndex].audio;
            this.audioContext.play();
        },

        stopPlay() {
			let that = this;
            if (this.audioContext) {
                this.audioContext.stop();
            }
            if (this.audioContext2) {
                this.audioContext2.stop();
            }
			this.playingList = [];
			this.playing = false;
			this.playingIndex = -1;
			uni.$emit('onPlayerEnd')
        },

        text2Audio(index) {
            let playingList = this.playingList;
            if (!playingList[index]) {
                return;
            }
            let text = playingList[index].text;
            this.setListState(index, 1);
			
			uni.showNavigationBarLoading();
			uni.showLoading({
			    title: '加载中'
			});
			text2Audio({
			    text: text
			}).then(res => {
				// console.log(res)
			    let playingList = this.playingList;
			    if (playingList[index]) {
			        playingList[index].audio = res[0];
			        playingList[index].state = 1;
			    	this.playingList = playingList;
			    }
				uni.hideLoading();
				uni.hideNavigationBarLoading();
			}).catch(err => {
			    this.stopPlay();
				uni.hideLoading();
				uni.hideNavigationBarLoading();
			})
        },

        setListState(index, state) {
            let playingList = this.playingList;
            if (!playingList[index]) {
                return;
            }
            playingList[index].state = state;
        
			this.playingList = playingList;
        },

        playAudio(audio) {
            this.stopPlay()
            this.audioContext2 = uni.createInnerAudioContext();
            uni.setInnerAudioOption({
                obeyMuteSwitch: false
            });
            this.audioContext2.src = audio;
            this.audioContext2.onEnded(() => {
                this.stopPlay();
				// console.log('结束')
                this.audioContext2 = null;
            });
            this.audioContext2.onError((res) => {
                this.stopPlay();
                this.audioContext2 = null;
				uni.showToast({
					title:'播放失败',
					icon:'none',
					duration:2000
				})
            });
            this.audioContext2.play();
			// console.log('开始')
        }
    },
    created: function () {}
};
</script>
<style></style>
